from langchain_core.prompts import PromptTemplate
from langchain.chains import LLMChain
from langchain_openai import OpenAI
from langchain.memory import ConversationBufferMemory

"""
>>> 需要使用openai 的 api_key
可以使用环境变量：OPENAI_API_KEY 或者调用OpenAI传参：OpenAI(openai_api_key="sk-xxx")
"""

prompt_template = """You are a chatbot having a conversation with a human.

{chat_history}
Human: {human_input}
Chatbot:"""

memory = ConversationBufferMemory(memory_key="chat_history")

prompt = PromptTemplate(input_variables=["chat_history", "human_input"], template=prompt_template)

llm_chain = LLMChain(
  llm=OpenAI(),
  prompt=prompt,
  verbose=True,
  memory=memory,
)

print(llm_chain.run("Who was the 5th president of the united states?"))
